package com.qf.course.web.controller;

import com.qf.course.core.entity.Course;
import com.qf.course.core.entity.CourseApply;
import com.qf.course.core.service.CourseService;
import com.qf.course.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/course")
@CrossOrigin(origins = "http://10.36.135.10:8848", allowCredentials = "true")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 查询全部课程列表
     * @return
     */
    @RequestMapping("/list")
    public R list(){
        List<Course> courses = courseService.queryAll();
        return R.create(courses);
    }

    /**
     * 根据学生id 查询课程列表 以及该课程和当前学生的关联状态（已经申请... 已经拒绝... 未申请...）
     * @return
     */
    @RequestMapping("/listByStu")
    public R listByStu(Integer uid){
        List<Course> courses = courseService.queryAllByStu(uid);
        return R.create(courses);
    }

    /**
     * 学生申请课程
     * type - 0 申请 1 取消申请
     * @return
     */
    @RequestMapping("/courseApply")
    public R courseApply(Integer uid, Integer cid, Integer type){
        int result = courseService.courseApply(uid, cid, type);
        return R.create(result);
    }

    /**
     * 查询学生的课程申请列表
     * @return
     */
    @RequestMapping("/queryCourseApply")
    public R queryCourseApply(Integer uid){
        List<CourseApply> applys = courseService.queryCourseApply(uid);
        return R.create(applys);
    }

    /**
     * 新增课程
     * @return
     */
    @RequestMapping("/insert")
    public R insert(Course course){
        courseService.insert(course);
        return R.create("succ");
    }

    /**
     * 批量删除课程
     * @return
     */
    @RequestMapping("/del")
    public R deletes(@RequestParam("ids[]") int[] ids){
        courseService.deletes(ids);
        return R.create("succ");
    }
}
